<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\Model\PlatformLimit;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;

class RiskController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header('风险提示配置');
            $content->description('');
            $content->body($this->grid());
        });
    }

    public function edit($id)
    {
        $content = new Content();
        $content->header('编辑');
        $content->body($this->form()->edit($id));
        return $content;
    }

    public function create()
    {
        return Admin::content(function (Content $content) {
            $content->header('添加风险提示配置');
            $content->description('');
            $content->body($this->form());
        });
    }

    protected function grid()
    {
        return Admin::grid(PlatformLimit::class, function (Grid $grid) {
            $grid->model()->orderBy('id', 'desc');
            $grid->id('记录ID');
            $grid->type('角色')->display(function ($data) {
                switch ($data) {
                    case 1:
                        return "充值弹窗";
                    case 2:
                        return "送礼物弹窗";
                }
            });
            $grid->value('限制的值');
            $grid->status('开关')->display(function ($data) {
                return $data == 1 ? '开' : '关';
            });
            $grid->created_at('创建时间');
            $grid->updated_at('更新时间');
        });
    }

    protected function form($mode = '', $id = '')
    {
        return Admin::form(PlatformLimit::class, function (Form $form) use ($mode, $id) {
            $form->select('type', '角色')->options([
                1 => '充值弹窗',
                2 => '送礼物弹窗'
            ])->required();
            $form->text('value', '限制的值')->required();
            $form->select('status', '开关')->options([
                0 => '关',
                1 => '开'
            ]);
        });
    }
}
